package cn.spring.ly.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.spring.ly.entity.Category;
import cn.spring.ly.page.PageInfo;
import cn.spring.ly.result.AjaxResult;
import cn.spring.ly.service.CategoryService;

@RequestMapping("/sys/cates")
@Controller
public class CategoryController {

	@Autowired
	CategoryService categoryService;

	@RequiresPermissions("cates:list")
	@RequestMapping("/list")
	public String cates(Model model, String cateName,
			@RequestParam(defaultValue = "1", required = false) Integer pageNo) {

		QueryWrapper<Category> queryWrapper = new QueryWrapper<Category>();

		if (!StringUtils.isEmpty(cateName))
			queryWrapper.like(true, Category.NAME, cateName);

		//List<Category> cateList = categoryService.list(queryWrapper);
		PageInfo<Category> pageInfo = new PageInfo<Category>(pageNo,10);
		pageInfo =  (PageInfo<Category>) categoryService.page(pageInfo, queryWrapper);
		//model.addAttribute("cateList", cateList);
		model.addAttribute("pageInfo", pageInfo);
		return "category_list";
	}

	@RequiresPermissions("cates:toAdd")
	@RequestMapping("/toAdd")
	public String toAdd(Model model) {
		QueryWrapper<Category> queryWrapper = new QueryWrapper<Category>();
		queryWrapper.eq(true,  Category.PARENT_ID, 0);
		List<Category> cateList = categoryService.list(queryWrapper);
		model.addAttribute("cateList", cateList);
		return "category_add";
	}

	@RequiresPermissions("cates:add")
	@ResponseBody
	@RequestMapping("/add")
	public AjaxResult addCate(Category category) {
		//检查分类名称是否存在
		QueryWrapper<Category> queryWrapper = new QueryWrapper<Category>();
		queryWrapper.eq(true,  Category.NAME, category.getName());
		//存在记录
		if(categoryService.count(queryWrapper) > 0){
			return AjaxResult.error("分类已存在，不能重复添加!");
		}
		
		boolean f = categoryService.save(category);
		if (f) {
			return AjaxResult.succes("添加成功");
		}
		return AjaxResult.error("添加失败");
	}

	@RequiresPermissions("cates:update")
	@ResponseBody
	@RequestMapping("/update")
	public AjaxResult updateCate(Category category) {
		//检查分类名称是否存在
		QueryWrapper<Category> queryWrapper = new QueryWrapper<Category>();
		queryWrapper.eq(true,  Category.NAME, category.getName());
		//去除自己
		queryWrapper.ne(true,  Category.ID, category.getId());
		
		//存在记录
		if(categoryService.count(queryWrapper) > 0){
			return AjaxResult.error("分类已存在，不能重复添加!");
		}
		boolean f = categoryService.updateById(category);
		if (f) {
			return AjaxResult.succes("更新成功");
		}
		return AjaxResult.error("更新失败");
	}

	@RequiresPermissions("cates:del")
	@ResponseBody
	@RequestMapping("/del/{id}")
	public AjaxResult delCate(@PathVariable Integer id) {

		boolean f = categoryService.removeById(id);
		if (f) {
			return AjaxResult.succes("删除成功");
		}
		return AjaxResult.error("删除失败");
	}
	
	@RequiresPermissions("cates:get")
	@RequestMapping("/get/{id}")
	public String getCate(@PathVariable Integer id,Model model){
		
		Category cate = categoryService.getById(id);
		QueryWrapper<Category> queryWrapper = new QueryWrapper<Category>();
		queryWrapper.eq(true,  Category.PARENT_ID, 0);
		List<Category> cateList = categoryService.list(queryWrapper);
		model.addAttribute("cateList", cateList);
		model.addAttribute("cate", cate);
		return "category_update";
	}
}
